<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.9.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

       <div class="layui-form layuimini-form">
			<div class="layui-form-item">
				<h2 align="center">用户信息</h2>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label required">账号</label>
			    <div class="layui-input-block">
			        <input type="text" name="id_number" lay-verify="required" lay-reqtext="账号不能为空" disabled="disabled" value="{{userid}}" class="layui-input" id="userid">
			        <!-- <tip>填写自己管理账号的名称。</tip> -->
			    </div>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label required">姓名</label>
			    <div class="layui-input-block">
			        <input type="text" name="user_name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
			    </div>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label">部门</label>
			    <div class="layui-input-block">
					<!-- <input type="text" name="department"  placeholder="请输入部门" value="" class="layui-input"> -->
					 <select name="department" id="department">
					 </select>
			    </div>
			</div>
			

			<div class="layui-form-item">
			    <label class="layui-form-label">邮箱</label>
			    <div class="layui-input-block">
			        <input type="email" name="email" placeholder="请输入邮箱" value="" class="layui-input">
			    </div>
			</div>
			
			
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../../static/lib/layui-v2.9.8/layui.js" charset="utf-8"></script>
<script src="../../static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','miniTab','layer'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab;
		var $ = layui.$;
		// ajax 头部增加csrf_token
		var token = "{{ csrf_token }}";
		$.ajaxSetup({
		    headers: { 
			 'X-CSRFTOKEN': `${token}`,
			 'AUTHORIZATION': 'Bearer ' + localStorage.token,
			 }, // JWTAuthentication.authenticate()方法获取 header = request.META.get(api_settings.AUTH_HEADER_NAME),
		});
		
		$.ajaxSetup({
			complete: function (XMLHttpRequest, textStatus) {
				if (XMLHttpRequest.status === 401) {  // 401 表示未授权，即 token 无效
					layer.msg('授权已过期，需重新登陆', { icon: 2, time: 2000 }, function () {
						window.location.href = '/login';
					})
				}
			}
		});
		
		var login_user = $("#login_user", top.document).text();

		$(function(){   //js 加载时，AJAX请求
			  $.ajax({
				url:'/user-info-query',
				type:'post',
				data:{'id_number':login_user},
				dataType:'json',
				success:function(result){
					console.log(result);
					var list = result.data;
					var dept = list.dept_info;
					var str = "<option value=''></option>"
					for (var i=0; i<dept.length;i++) {
						str += "<option value=\""+dept[i].id+"\">"+ dept[i].name+"</option>"
					}
					// console.log(str);
					$("input[name='id_number']").val(list.id_number)
					$("input[name='user_name']").val(list.user_name)
					$("input[name='email']").val(list.email)
					$('#department').html(str);
					$('#department option').filter(function(){return $(this).val()==""+list.department+"";}).attr("selected",true); //精确匹配
					form.render('select')   //重新渲染 form select内容

				},
				error:function(){
					layer.msg('部门查询API出错！');
				}
			})
		});


        //监听提交
        form.on('submit(saveBtn)', function (data) {
			$.ajax({
				url:'/user-setting-save',
				type:'post',
				data:data.field,
				dataType:'json',
				success:function(result){
					if (result.success){
						layer.msg(result.msg, { icon: 1, time: 2000 }, function () {
						  miniTab.deleteCurrentByIframe();
						  parent.layui.table.reload('currentTableId');
						})
					}
					else{
						layer.alert(result.msg);
					}
				},
				error:function(){
					layer.msg('API出错！');
				}
			});
			
            return false;
        });

    });
</script>
</body>
</html>